package pt.ua.helpdesk.client;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * This class extends a composite and will allow the
 * user to view pending tickets.
 * 
 * @author Amigos do Noddy
 */
public class TicketsViewPending extends Composite {

	/**
	 * Class constructor
	 */
	public TicketsViewPending() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		
		ServiceTickets.Util.getInstance().getPendingTickets(new ViewPendingTicketsAsyncCallback(verticalPanel,new Integer(Default.getSession("IDPerson").toString())));
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the view
	 * pending tickets service service
	 * 
	 * @author Amigos do Noddy
	 */
	static class ViewPendingTicketsAsyncCallback implements AsyncCallback 
	{
		  VerticalPanel vp;
		  private Integer personLvl;
		  
		  public ViewPendingTicketsAsyncCallback(VerticalPanel vp, Integer lvl) {
			  this.vp= vp;
			  this.personLvl = lvl;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  ArrayList Tickets= (ArrayList)object;
			  ArrayList idTickets = (ArrayList)Tickets.get(0);
			  ArrayList titles = (ArrayList)Tickets.get(1);
			  ArrayList dates = (ArrayList)Tickets.get(2);
			  //Integer idAssistant= new Integer(1);
			  
			  Grid grid = new Grid();
			  
			  System.out.println("Criou a grid");
			  grid.resize(idTickets.size()+1, 6);
			  grid.setStyleName("grid");
			  grid.setBorderWidth(1);
			  grid.setCellSpacing(4);
			  			  
			  Label lbl1 = new Label();
			  lbl1.setText("#");
			  lbl1.setStyleName("section_title");
			  grid.setWidget(0, 0, lbl1);
			  
			  Label lbl2 = new Label();
			  lbl2.setText("Título");
			  lbl2.setStyleName("section_title");
			  grid.setWidget(0, 1, lbl2);
			  		  
			  Label lbl3 = new Label();
			  lbl3.setText("Estado");
			  lbl3.setStyleName("section_title");
			  grid.setWidget(0, 2, lbl3);
			  //ver
			  Label lbl4 = new Label();
			  lbl4.setText("");
			  lbl4.setStyleName("section_title");
			  grid.setWidget(0, 3, lbl4);
			  //remover
			  Label lbl5 = new Label();
			  lbl5.setText("");
			  lbl5.setStyleName("section_title");
			  grid.setWidget(0, 4, lbl5);
			  //assign
			  Label lbl6 = new Label();
			  lbl6.setText("");
			  lbl6.setStyleName("section_title");
			  grid.setWidget(0, 5, lbl6);
			  System.out.println("labels");
			  for(int i = 0; i < idTickets.size(); i++)
			  {		  
				  Label lblCount = new Label();
				  lblCount.setText(""+(i+1));
				  lblCount.setStyleName("label");
				  grid.setWidget(i+1, 0, lblCount);
				  
				  Label lblTitle = new Label();
				  lblTitle.setText(titles.get(i).toString());
				  lblTitle.setStyleName("label");
				  grid.setWidget(i+1, 1, lblTitle);
				  
				  Label lblDate = new Label();
				  lblDate.setText(dates.get(i).toString());
				  lblDate.setStyleName("label");
				  grid.setWidget(i+1, 2, lblDate);
				  
				  		  			  
				  Button btnVer = new Button();
				  btnVer.setText("Ver");
				  btnVer.setStyleName("button");
				  btnVer.addClickListener(new TicketViewOnClick((Integer)idTickets.get(i)));
				  grid.setWidget(i+1, 3, btnVer);
				  
				  Button btnRemove = new Button("Remover");
				  btnRemove.setStyleName("button");
				  btnRemove.addClickListener(new TicketRemoveOnClick((Integer)idTickets.get(i)));
				  grid.setWidget(i+1, 4, btnRemove);
				  
				  if(personLvl.intValue() > 2)
    			  {
    				  Button btnAssign = new Button("Atribuir");
	    			  btnAssign.setStyleName("button");
	    			  btnAssign.addClickListener(new TicketAssignOnClick((Integer)idTickets.get(i)));
	    			  grid.setWidget(i+1, 5, btnAssign);
    			  }
				  System.out.println("Criou"); 
			  }
			  
			  vp.add(grid);
			     	
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		  
		}

}
